package m.jclecai.com.pop;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import butterknife.ButterKnife;
import m.jclecai.com.R;

/**
 * Created by zd on 16/6/21.
 */
public abstract class BasePopupWindow extends PopupWindow {

    WindowManager mWindowManager;

    ViewGroup mView;
    Context mContext;

    public View getView() {
        return mView;
    }

    public Context getContext() {
        return mContext;
    }

    public BasePopupWindow(Context context) {
        super(context);
        this.mContext = context;
        this.mWindowManager = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE));
    }

    public int getScreenWidth() {
        return mWindowManager.getDefaultDisplay().getWidth();
    }

    public int getScreenHeight() {
        return mWindowManager.getDefaultDisplay().getHeight();
    }

    protected <T extends View> T findViewById(@IdRes int id) {
        return (T) mView.findViewById(id);
    }

    protected void setContentView(@LayoutRes int resource) {
        //制作容器
        mView = new FrameLayout(getContext());
        //设置容器获得点击事件 否则容器下层视图会被触发
        mView.setClickable(true);
        mView.setFocusable(true);
        //装载视图
        final View inflate = LayoutInflater.from(mContext).inflate(resource, null);

        mView.addView(inflate);


        inflate.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        inflate.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;


        mView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                mView.post(new Runnable() {
                    @Override
                    public void run() {
                        inflate.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.pop_fade_in));
                    }
                });
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
            }
        });
        //设置SelectPicPopupWindow的View
        setContentView(mView);
        ButterKnife.bind(this, mView);
//        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        ColorDrawable dw;
        if (allowBackgroundTranslucent()) {
            //实例化一个ColorDrawable颜色为半透明
            dw = new ColorDrawable(0x90000000);
        } else {
            dw = new ColorDrawable(0x00000000);
        }
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //虚拟按键
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        int style = setPopWindowAnimationStyle();
        if (style <= 0)
            style = R.style.popup_animation;
        setAnimationStyle(style);
    }


    @Override
    public void dismiss() {
        int style = setPopWindowAnimationStyle();
        if (style > 0)
            super.dismiss();
        else
            mView.post(new Runnable() {
                @Override
                public void run() {
                    Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.pop_fade_out);
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            mView.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    close();
                                }
                            }, 300);
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
                    mView.getChildAt(0).startAnimation(animation);
                }
            });
    }

    private void close() {
        super.dismiss();
    }

    //true有背景色 false无背景色
    protected abstract boolean allowBackgroundTranslucent();

    //设置显示动画 不加的话默认为-1
    protected abstract int setPopWindowAnimationStyle();

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }
}
